package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityCouponDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityCoupon;
import com.ecoolex.ying.space.entity.EstateProjectActivityCoupon.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponExample.Criteria;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityCouponManager {
	
    @Autowired
    private EstateProjectActivityCouponDAO dao;
    
    public EstateProjectActivityCoupon getEstateProjectActivityCoupon(Integer couponId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(couponId, EstateProjectActivityCoupon.NOT_DELETED);
	}

	public Pagination<EstateProjectActivityCoupon> queryEstateProjectActivityCoupon(Sorting[] sorts, Page page) {
		EstateProjectActivityCouponExample example = new EstateProjectActivityCouponExample();
		Criteria criteria = example.createCriteria();
		criteria.andDeletedEqualTo(EstateProjectActivityCoupon.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public EstateProjectActivityCoupon getEstateProjectActivityCouponOnline(Integer couponId) {
		EstateProjectActivityCouponExample example = new EstateProjectActivityCouponExample();
		Criteria criteria = example.createCriteria();
		criteria.andCouponIdEqualTo(couponId)
				.andDeletedEqualTo(EstateProjectActivityCoupon.NOT_DELETED)
				.andOnlineEqualTo(true);
		return dao.selectOneByExample(example);
	}

	public Pagination<EstateProjectActivityCoupon> queryEstateProjectActivityCouponOnline(Page page) {
		EstateProjectActivityCouponExample example = new EstateProjectActivityCouponExample();
		Criteria criteria = example.createCriteria();
		criteria.andOnlineEqualTo(true)
				.andDeletedEqualTo(EstateProjectActivityCoupon.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.facePrice.asc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	public EstateProjectActivityCoupon addEstateProjectActivityCoupon(EstateProjectActivityCoupon record, String createUser) {
		record.setOnline(false);
		record.setDeleted(EstateProjectActivityCoupon.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
	}

	public EstateProjectActivityCoupon editEstateProjectActivityCoupon(EstateProjectActivityCoupon record, String updateUser) {
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.deleted,Column.online,Column.createTime,Column.createUser));
		return record;
	}

	public void delEstateProjectActivityCoupon(EstateProjectActivityCoupon record, String updateUser) {
		record.setDeleted(EstateProjectActivityCoupon.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateTime,Column.updateUser);
	}

	public void onlineEstateProjectActivityCoupon(EstateProjectActivityCoupon record, String updateUser) {
		record.setOnline(true);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateTime,Column.updateUser);
	}

	public void offlineEstateProjectActivityCoupon(EstateProjectActivityCoupon record, String updateUser) {
		record.setOnline(false);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateTime,Column.updateUser);
	}
}